//
//  CContactMgr.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/21.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CContactMgr.h"
#import "CContactModel.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"
#import "CDepartmentMgr.h"
#import "CDepartmentModel.h"

@implementation CContactMgr

//插入联系人
-(BOOL)insetContact:(CContactModel *)contact
{
    if(contact == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(contact.UserID)) {
            return NO;
        }
    }
    
    
    __block BOOL ret = NO;
    
    NSString *sql = nil;
    sql = [NSString stringWithFormat:@"select * from CONTACT where  UserID = \'%@\'", contact.UserID];

    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        FMResultSet *result = [db executeQuery:sql];
        if (result && [result next]) {
            ret = YES;
        }
        [result close];
      
        if (ret) {
             ret = [db executeUpdate:@"UPDATE CONTACT SET DEPTID = ?, UserName = ? , UserCode = ?, Extension = ?, Autograph = ?, Position = ?, RoleID = ?, Telephone = ?, MobileTelephone = ?, Email = ?, HeadPortrait = ? , DEPNAME = ? , SEX = ? , isDel = ? , birthDay = ? , address = ?, GroupID = ?, IsAdmin = ?, IsAvailable = ?, IsOftenContact = ? ,info = ?,img = ?,msgNoticeSet = ?,Source = ?, PinYin = ?, messageSettingType = ?,compCode = ?,companyTelePhone = ?,pcAvailable = ? ,IsSelected = ?,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where UserID = ?", M_STOREVALUE(contact.DEPTID), M_STOREVALUE(contact.UserName), M_STOREVALUE(contact.UserCode), M_STOREVALUE(contact.EXtenSion), M_STOREVALUE(contact.Autograph), M_STOREVALUE(contact.Position), M_STOREVALUE(contact.RoleID), M_STOREVALUE(contact.Telephone), M_STOREVALUE(contact.MobileTelephone), M_STOREVALUE(contact.Email), M_STOREVALUE(contact.HeadPortrait), M_STOREVALUE(contact.DEPNAME),M_STOREVALUE(contact.SEX),contact.isDel?@1:@0,M_STOREVALUE(contact.birthDay),M_STOREVALUE(contact.address),M_STOREVALUE(contact.GroupID),contact.IsAdmin?@1:@0,M_STOREVALUE(contact.IsAvailable),@(contact.IsOftenContact),M_STOREVALUE(contact.info),M_STOREVALUE(contact.img),M_STOREVALUE(contact.msgNoticeSet),M_STOREVALUE(contact.Source),M_STOREVALUE(contact.PinYin),@(contact.messageSettingType),M_STOREVALUE(contact.compCode),M_STOREVALUE(contact.companyTelePhone),M_STOREVALUE(contact.pcAvailable), contact.isSelected?@1:@0,M_STOREVALUE(contact.yuLiuZiduan1),@(contact.yuLiuZiduan2),M_STOREVALUE(contact.yuLiuZiduan3),M_STOREVALUE(contact.UserID)];
        } else {
           ret = [db executeUpdate:@"INSERT INTO CONTACT(DEPTID, UserName, UserCode, UserID, Extension, Autograph, Position, RoleID, Telephone, MobileTelephone, Email, HeadPortrait, DEPNAME, SEX, isDel, birthDay, address, GroupID, IsAdmin, IsAvailable,IsOftenContact,info,img,msgNoticeSet,Source,PinYin,messageSettingType,compCode,companyTelePhone,pcAvailable,firstLetter,IsSelected,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", M_STOREVALUE(contact.DEPTID), M_STOREVALUE(contact.UserName), M_STOREVALUE(contact.UserCode), M_STOREVALUE(contact.UserID), M_STOREVALUE(contact.EXtenSion), M_STOREVALUE(contact.Autograph), M_STOREVALUE(contact.Position), M_STOREVALUE(contact.RoleID), M_STOREVALUE(contact.Telephone), M_STOREVALUE(contact.MobileTelephone), M_STOREVALUE(contact.Email), M_STOREVALUE(contact.HeadPortrait),M_STOREVALUE(contact.DEPNAME),M_STOREVALUE(contact.SEX),contact.isDel?@1:@0,M_STOREVALUE(contact.birthDay),M_STOREVALUE(contact.address),M_STOREVALUE(contact.GroupID),contact.IsAdmin?@1:@0,M_STOREVALUE(contact.IsAvailable),@(contact.IsOftenContact),M_STOREVALUE(contact.info),M_STOREVALUE(contact.img),M_STOREVALUE(contact.msgNoticeSet),M_STOREVALUE(contact.Source),M_STOREVALUE(contact.PinYin),@(contact.messageSettingType),M_STOREVALUE(contact.compCode),M_STOREVALUE(contact.companyTelePhone),M_STOREVALUE(contact.pcAvailable),M_STOREVALUE(contact.firstLetter),contact.isSelected?@1:@0,M_STOREVALUE(contact.yuLiuZiduan1),@(contact.yuLiuZiduan2),M_STOREVALUE(contact.yuLiuZiduan3)];
        }
        if ([contact.yuLiuZiduan3 isEqualToString:@"isWaibu"]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:@"K_XMPPDIDGETWAIBULIST" object:nil];
            });
        }
       
    }];
    return ret;
}
//批量插入联系人
-(BOOL)batchInsertContact:(NSArray *)contactList
{
    
    if(contactList == nil && [contactList count] <= 0)
    {
        return NO;
    }
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CContactModel *contact in contactList)
        {
            if (!M_ISSTRNULL(contact.UserID)) {
                ret = [db executeUpdate:@"INSERT INTO CONTACT(DEPTID, UserName, UserCode, UserID, Extension, Autograph, Position, RoleID, Telephone, MobileTelephone, Email, HeadPortrait, DEPNAME, SEX, isDel, birthDay, address, GroupID, IsAdmin, IsAvailable,IsOftenContact,info,img,msgNoticeSet,Source,PinYin,messageSettingType,compCode,companyTelePhone,pcAvailable,firstLetter,IsSelected,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", M_STOREVALUE(contact.DEPTID), M_STOREVALUE(contact.UserName), M_STOREVALUE(contact.UserCode), M_STOREVALUE(contact.UserID), M_STOREVALUE(contact.EXtenSion), M_STOREVALUE(contact.Autograph), M_STOREVALUE(contact.Position), M_STOREVALUE(contact.RoleID), M_STOREVALUE(contact.Telephone), M_STOREVALUE(contact.MobileTelephone), M_STOREVALUE(contact.Email), M_STOREVALUE(contact.HeadPortrait),M_STOREVALUE(contact.DEPNAME),M_STOREVALUE(contact.SEX),contact.isDel?@1:@0,M_STOREVALUE(contact.birthDay),M_STOREVALUE(contact.address),M_STOREVALUE(contact.GroupID),contact.IsAdmin?@1:@0,M_STOREVALUE(contact.IsAvailable),@(contact.IsOftenContact),M_STOREVALUE(contact.info),M_STOREVALUE(contact.img),M_STOREVALUE(contact.msgNoticeSet),M_STOREVALUE(contact.Source),M_STOREVALUE(contact.PinYin),@(contact.messageSettingType),M_STOREVALUE(contact.compCode),M_STOREVALUE(contact.companyTelePhone),M_STOREVALUE(contact.pcAvailable),M_STOREVALUE(contact.firstLetter),contact.isSelected?@1:@0,M_STOREVALUE(contact.yuLiuZiduan1),@(contact.yuLiuZiduan2),M_STOREVALUE(contact.yuLiuZiduan3)];
                if(!ret)
                {
                    *rollback = YES;
                }

            }
        }
 
    }];
    return ret;
}
//批量更新联系人
-(BOOL)batchUpdateContact:(NSArray *)contactList
{
    if(contactList == nil || contactList.count <= 0)
    {
        return NO;
    }
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CContactModel *contact in contactList)
        {
            ret = [db executeUpdate:@"UPDATE CONTACT SET DEPTID = ?, UserName = ? , UserCode = ?, Extension = ?, Autograph = ?, Position = ?, RoleID = ?, Telephone = ?, MobileTelephone = ?, Email = ?, HeadPortrait = ? , DEPNAME = ? , SEX = ? , isDel = ? , birthDay = ? , address = ?, GroupID = ?, IsAdmin = ?, IsAvailable = ?, IsOftenContact = ? ,info = ?,img = ?,msgNoticeSet = ?,Source = ?, PinYin = ?,messageSettingType = ?,compCode = ?,companyTelePhone = ?,pcAvailable = ?, firstLetter = ? ,IsSelected = ?,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where UserID = ?", M_STOREVALUE(contact.DEPTID), M_STOREVALUE(contact.UserName), M_STOREVALUE(contact.UserCode), M_STOREVALUE(contact.EXtenSion), M_STOREVALUE(contact.Autograph), M_STOREVALUE(contact.Position), M_STOREVALUE(contact.RoleID), M_STOREVALUE(contact.Telephone), M_STOREVALUE(contact.MobileTelephone), M_STOREVALUE(contact.Email), M_STOREVALUE(contact.HeadPortrait), M_STOREVALUE(contact.DEPNAME),M_STOREVALUE(contact.SEX),contact.isDel?@1:@0,M_STOREVALUE(contact.birthDay),M_STOREVALUE(contact.address),M_STOREVALUE(contact.GroupID),contact.IsAdmin?@1:@0,M_STOREVALUE(contact.IsAvailable),@(contact.IsOftenContact),M_STOREVALUE(contact.info),M_STOREVALUE(contact.img),M_STOREVALUE(contact.msgNoticeSet),M_STOREVALUE(contact.Source),M_STOREVALUE(contact.PinYin),@(contact.messageSettingType),M_STOREVALUE(contact.compCode),M_STOREVALUE(contact.companyTelePhone),M_STOREVALUE(contact.pcAvailable),M_STOREVALUE(contact.firstLetter),contact.isSelected?@1:@0,M_STOREVALUE(contact.yuLiuZiduan1),@(contact.yuLiuZiduan2),M_STOREVALUE(contact.yuLiuZiduan3), M_STOREVALUE(contact.UserID)];
            if(!ret)
            {
                *rollback = YES;
            }
        }

    }];
    return ret;
}



//删除联系人
-(BOOL)delContact:(CContactModel *)contact
{
    if(contact == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from CONTACT where identifer = ?", @(contact.ContactIndex)];
    }];
    return ret;
}

- (BOOL)delAllDelContact
{

//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from CONTACT where isDel = 0 AND yuliuZiduan3 = ?",@""];
    }];
    return ret;

}

//删除所有联系人
-(BOOL)delAllContact
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from CONTACT"];
    }];
    return ret;
}

//  删除客服联系人
- (void)deleAllKeFuContact
{
    __block BOOL ret = NO;
//    NSString *sql = nil;
//    sql = [NSString stringWithFormat:@"delete from CONTACT where yuLiuZiduan3 = \'%@\' AND isDel = 0", @"isKefu"];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
//        ret = [db executeUpdate:sql];
        ret = [db executeUpdate:@"UPDATE CONTACT SET yuLiuZiduan3 = ? where yuLiuZiduan3 = ?",@"",@"isKefu"];

    }];

}
//更新联系人
-(BOOL)updateContact:(CContactModel *)contact
{
    if(contact == nil)
    {
        return NO;
    }
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE CONTACT SET DEPTID = ?, UserName = ? , UserCode = ?, Extension = ?, Autograph = ?, Position = ?, RoleID = ?, Telephone = ?, MobileTelephone = ?, Email = ?, HeadPortrait = ? , DEPNAME = ? , SEX = ? , isDel = ? , birthDay = ? , address = ?, GroupID = ?, IsAdmin = ?, IsAvailable = ?, IsOftenContact = ? ,info = ?,img = ?,msgNoticeSet = ?,Source = ?, PinYin = ?, messageSettingType = ?,compCode = ?,companyTelePhone = ?,pcAvailable = ? ,IsSelected = ?,yuLiuZiduan1 = ?,yuLiuZiduan2 = ?,yuLiuZiduan3 = ? where UserID = ?", M_STOREVALUE(contact.DEPTID), M_STOREVALUE(contact.UserName), M_STOREVALUE(contact.UserCode), M_STOREVALUE(contact.EXtenSion), M_STOREVALUE(contact.Autograph), M_STOREVALUE(contact.Position), M_STOREVALUE(contact.RoleID), M_STOREVALUE(contact.Telephone), M_STOREVALUE(contact.MobileTelephone), M_STOREVALUE(contact.Email), M_STOREVALUE(contact.HeadPortrait), M_STOREVALUE(contact.DEPNAME),M_STOREVALUE(contact.SEX),contact.isDel?@1:@0,M_STOREVALUE(contact.birthDay),M_STOREVALUE(contact.address),M_STOREVALUE(contact.GroupID),contact.IsAdmin?@1:@0,M_STOREVALUE(contact.IsAvailable),@(contact.IsOftenContact),M_STOREVALUE(contact.info),M_STOREVALUE(contact.img),M_STOREVALUE(contact.msgNoticeSet),M_STOREVALUE(contact.Source),M_STOREVALUE(contact.PinYin),@(contact.messageSettingType),M_STOREVALUE(contact.compCode),M_STOREVALUE(contact.companyTelePhone),M_STOREVALUE(contact.pcAvailable), contact.isSelected?@1:@0,M_STOREVALUE(contact.yuLiuZiduan1),@(contact.yuLiuZiduan2),M_STOREVALUE(contact.yuLiuZiduan3),M_STOREVALUE(contact.UserID)];
    }];
    return ret;
}

- (CContactModel *)loadContactWithUserId:(NSString *)userId
{
    if(M_ISSTRNULL(userId))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CContactModel *contact = nil;
    sql = [NSString stringWithFormat:@"select * from CONTACT where  UserID = \'%@\'", userId ];
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            contact = [[CContactModel alloc] init];
            contact.ContactIndex = [result  longLongIntForColumn:@"identifer"];
            contact.DEPTID = [result stringForColumn:@"DEPTID"];
            contact.UserName = [result stringForColumn:@"UserName"];
            contact.UserCode = [result stringForColumn:@"UserCode"];
            contact.UserID = [result stringForColumn:@"UserID"];
            contact.EXtenSion = [result stringForColumn:@"Extension"];
            contact.Autograph = [result stringForColumn:@"Autograph"];
            contact.Position = [result stringForColumn:@"Position"];
            contact.RoleID = [result stringForColumn:@"RoleID"];
            contact.Telephone = [result stringForColumn:@"Telephone"];
            contact.MobileTelephone = [result stringForColumn:@"MobileTelephone"];
            contact.Email = [result stringForColumn:@"Email"];
            contact.HeadPortrait = [result stringForColumn:@"HeadPortrait"];
            contact.DEPNAME = [result stringForColumn:@"DEPNAME"];
            contact.SEX = [result stringForColumn:@"SEX"];
            contact.isDel = [result intForColumn:@"isDel"];
            contact.address = [result stringForColumn:@"address"];
            contact.birthDay = [result stringForColumn:@"birthDay"];
            contact.GroupID = [result stringForColumn:@"GroupID"];
            contact.IsAdmin = [result boolForColumn:@"IsAdmin"];
            contact.IsAvailable = [result stringForColumn:@"IsAvailable"];
            contact.pcAvailable = [result stringForColumn:@"pcAvailable"];
            contact.IsOftenContact = [result intForColumn:@"IsOftenContact"];
            contact.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
            contact.info = [result stringForColumn:@"info"];
            contact.img = [result stringForColumn:@"img"];
            contact.Source = [result stringForColumn:@"Source"];
            contact.PinYin = [result stringForColumn:@"PinYin"];
            contact.messageSettingType = [result intForColumn:@"messageSettingType"];
            contact.companyTelePhone = [result stringForColumn:@"companyTelePhone"];
            contact.compCode = [result stringForColumn:@"compCode"];
            contact.isSelected = [result boolForColumn:@"isSelected"];
            contact.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            contact.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            contact.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
        //        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
        //        contact.DEPNAME = dep.DepartName;
        [result close];
    }];
    return contact;

}

- (BOOL)isWaibuContactWithUserId:(NSString *)userId
{
    if(M_ISSTRNULL(userId))
    {
        return nil;
    }
    __block NSString *yuliuZiduan3 = nil;
    NSString *sql = nil;
    sql = [NSString stringWithFormat:@"select yuLiuZiduan3 from CONTACT where  UserID = \'%@\' AND isDel = 0",userId];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next] && result) {
            yuliuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
        [result close];
    }];
    if ([yuliuZiduan3 isEqualToString:@"isWaibu"]) {
        return YES;
    } else {
        return NO;
    }
}
- (BOOL)isKefuContactWithUserId:(NSString *)userId
{
    if(M_ISSTRNULL(userId))
    {
        return nil;
    }
    __block NSString *yuliuZiduan3 = nil;
    NSString *sql = nil;
    sql = [NSString stringWithFormat:@"select yuLiuZiduan3 from CONTACT where  UserID = \'%@\' AND isDel = 0",userId];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next] && result) {
            yuliuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
        [result close];
    }];
    if ([yuliuZiduan3 isEqualToString:@"isKefu"]) {
        return YES;
    } else {
        return NO;
    }

}
- (CContactModel *)loadNotDelContactWithUserId:(NSString *)userId
{
    if(M_ISSTRNULL(userId))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CContactModel *contact = nil;
    sql = [NSString stringWithFormat:@"select * from CONTACT where  UserID = \'%@\' AND (isDel = 1 OR yuLiuZiduan3 = \'%@\' OR yuLiuZiduan3 = \'%@\')",userId ,@"isKefu", @"isWaibu"];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            contact = [[CContactModel alloc] init];
            contact.ContactIndex = [result  longLongIntForColumn:@"identifer"];
            contact.DEPTID = [result stringForColumn:@"DEPTID"];
            contact.UserName = [result stringForColumn:@"UserName"];
            contact.UserCode = [result stringForColumn:@"UserCode"];
            contact.UserID = [result stringForColumn:@"UserID"];
            contact.EXtenSion = [result stringForColumn:@"Extension"];
            contact.Autograph = [result stringForColumn:@"Autograph"];
            contact.Position = [result stringForColumn:@"Position"];
            contact.RoleID = [result stringForColumn:@"RoleID"];
            contact.Telephone = [result stringForColumn:@"Telephone"];
            contact.MobileTelephone = [result stringForColumn:@"MobileTelephone"];
            contact.Email = [result stringForColumn:@"Email"];
            contact.HeadPortrait = [result stringForColumn:@"HeadPortrait"];
            contact.DEPNAME = [result stringForColumn:@"DEPNAME"];
            contact.SEX = [result stringForColumn:@"SEX"];
            contact.isDel = [result intForColumn:@"isDel"];
            contact.address = [result stringForColumn:@"address"];
            contact.birthDay = [result stringForColumn:@"birthDay"];
            contact.GroupID = [result stringForColumn:@"GroupID"];
            contact.IsAdmin = [result boolForColumn:@"IsAdmin"];
            contact.IsAvailable = [result stringForColumn:@"IsAvailable"];
            contact.pcAvailable = [result stringForColumn:@"pcAvailable"];
            contact.IsOftenContact = [result intForColumn:@"IsOftenContact"];
            contact.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
            contact.info = [result stringForColumn:@"info"];
            contact.img = [result stringForColumn:@"img"];
            contact.Source = [result stringForColumn:@"Source"];
            contact.PinYin = [result stringForColumn:@"PinYin"];
            contact.messageSettingType = [result intForColumn:@"messageSettingType"];
            contact.companyTelePhone = [result stringForColumn:@"companyTelePhone"];
            contact.compCode = [result stringForColumn:@"compCode"];
            contact.isSelected = [result boolForColumn:@"isSelected"];
            contact.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            contact.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            contact.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
        }
        //        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
        //        contact.DEPNAME = dep.DepartName;
        [result close];
    }];
    return contact;

}

- (NSString *)loadContactNameWithUserId:(NSString *)userId
{
    if(M_ISSTRNULL(userId))
    {
        return nil;
    }
    NSString *sql = nil;
    __block NSString *userName;
    sql = [NSString stringWithFormat:@"select UserName from CONTACT where  UserID = \'%@\'", userId];
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            userName = [result stringForColumn:@"UserName"];
        }
        //        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
        //        contact.DEPNAME = dep.DepartName;
        [result close];
    }];
    return userName;

}

- (CContactModel *)loadCurContact
{
    CAccountModel *model = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount];
    CContactModel *contact = [self loadContactWithUserCode:model.userName];
//    CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
//    contact.DEPNAME = dep.DepartName;
    return contact;
}

- (BOOL)loadAllContact
{
    NSString *sql = nil;
    __block BOOL ret = NO;
    sql = [NSString stringWithFormat:@"select * from CONTACT where isDel = 1"];
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        if(result == nil || ![result next]) {
            ret = NO;
        } else {
            ret = YES;
        }
        
        [result close];
    }];
    return ret;

}

- (BOOL)updateAllContactStatus
{
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE CONTACT SET IsAvailable = ?, pcAvailable = ?;",@"离线",@"离线"];
        
    }];
    return ret;

}
//根据联系人code加载联系人
-(CContactModel *)loadContactWithUserCode:(NSString *)contactCode
{
    
    if(M_ISSTRNULL(contactCode))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CContactModel *contact = nil;
    sql = [NSString stringWithFormat:@"select * from CONTACT where  UserCode = \'%@\' AND isDel = 1", contactCode ];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            contact = [[CContactModel alloc] init];
            contact.ContactIndex = [result  longLongIntForColumn:@"identifer"];
            contact.DEPTID = [result stringForColumn:@"DEPTID"];
            contact.UserName = [result stringForColumn:@"UserName"];
            contact.UserCode = [result stringForColumn:@"UserCode"];
            contact.UserID = [result stringForColumn:@"UserID"];
            contact.EXtenSion = [result stringForColumn:@"Extension"];
            contact.Autograph = [result stringForColumn:@"Autograph"];
            contact.Position = [result stringForColumn:@"Position"];
            contact.RoleID = [result stringForColumn:@"RoleID"];
            contact.Telephone = [result stringForColumn:@"Telephone"];
            contact.MobileTelephone = [result stringForColumn:@"MobileTelephone"];
            contact.Email = [result stringForColumn:@"Email"];
            contact.HeadPortrait = [result stringForColumn:@"HeadPortrait"];
            contact.DEPNAME = [result stringForColumn:@"DEPNAME"];
            contact.SEX = [result stringForColumn:@"SEX"];
            contact.isDel = [result intForColumn:@"isDel"];
            contact.address = [result stringForColumn:@"address"];
            contact.birthDay = [result stringForColumn:@"birthDay"];
            contact.GroupID = [result stringForColumn:@"GroupID"];
            contact.IsAdmin = [result boolForColumn:@"IsAdmin"];
            contact.IsAvailable = [result stringForColumn:@"IsAvailable"];
            contact.pcAvailable = [result stringForColumn:@"pcAvailable"];
            contact.IsOftenContact = [result intForColumn:@"IsOftenContact"];
            contact.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
            contact.info = [result stringForColumn:@"info"];
            contact.img = [result stringForColumn:@"img"];
             contact.Source = [result stringForColumn:@"Source"];
            contact.PinYin = [result stringForColumn:@"PinYin"];
            contact.messageSettingType = [result intForColumn:@"messageSettingType"];
            contact.companyTelePhone = [result stringForColumn:@"companyTelePhone"];
            contact.compCode = [result stringForColumn:@"compCode"];
            contact.isSelected = [result boolForColumn:@"isSelected"];
            contact.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            contact.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            contact.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

        }
//        CDepartmentModel *dep = [[[CDepartmentMgr alloc] init] loadDepartment:contact.DEPTID];
//        contact.DEPNAME = dep.DepartName;
       [result close];
    }];
    return contact;
}

//查询客服联系人列表
- (NSArray *)loadKeFuList
{
    NSString *sql = nil;
    __block NSMutableArray *contactList = [NSMutableArray arrayWithCapacity:10];
    sql = [NSString stringWithFormat:@"select * from CONTACT where yuLiuZiduan3 = \'%@\'", @"isKefu"];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CContactModel *contact = [[CContactModel alloc] init];
            contact.ContactIndex = [result longLongIntForColumn:@"identifer"];
            contact.UserName = [result stringForColumn:@"UserName"];
            contact.UserCode = [result stringForColumn:@"UserCode"];
            contact.UserID = [result stringForColumn:@"UserID"];
            contact.PinYin = [result stringForColumn:@"PinYin"];
            contact.messageSettingType = [result intForColumn:@"messageSettingType"];
            contact.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            
            [contactList addObject:contact];
            contact = nil;
        }
        [result close];
    }];
    return contactList;

}

//查询外部联系人
- (NSArray *)loadWaiBuList
{
    NSString *sql = nil;
    __block NSMutableArray *contactList = [NSMutableArray arrayWithCapacity:10];
    sql = [NSString stringWithFormat:@"select * from CONTACT where yuLiuZiduan3 = \'%@\'", @"isWaibu"];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CContactModel *contact = [[CContactModel alloc] init];
            contact.ContactIndex = [result longLongIntForColumn:@"identifer"];
            contact.UserName = [result stringForColumn:@"UserName"];
            contact.UserCode = [result stringForColumn:@"UserCode"];
            contact.UserID = [result stringForColumn:@"UserID"];
            contact.PinYin = [result stringForColumn:@"PinYin"];
            contact.messageSettingType = [result intForColumn:@"messageSettingType"];
            contact.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            contact.compCode = [result stringForColumn:@"compCode"];
            [contactList addObject:contact];
            contact = nil;
        }
        [result close];
    }];
    return contactList;

}
//根据部门id加载联系人
-(NSArray *)loadContactByDeptID:(NSString *)departID
{
    if(M_ISSTRNULL(departID))
    {
        return nil;
    }
    NSString *sql = nil;
    __block NSMutableArray *contactList = [NSMutableArray arrayWithCapacity:10];
    sql = [NSString stringWithFormat:@"select * from CONTACT where  DEPTID = \'%@\' AND isDel = 1", departID ];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CContactModel *contact = [[CContactModel alloc] init];
            contact.ContactIndex = [result longLongIntForColumn:@"identifer"];
            contact.DEPTID = [result stringForColumn:@"DEPTID"];
            contact.UserName = [result stringForColumn:@"UserName"];
            contact.UserCode = [result stringForColumn:@"UserCode"];
            contact.UserID = [result stringForColumn:@"UserID"];
            contact.EXtenSion = [result stringForColumn:@"Extension"];
            contact.Autograph = [result stringForColumn:@"Autograph"];
            contact.Position = [result stringForColumn:@"Position"];
            contact.RoleID = [result stringForColumn:@"RoleID"];
            contact.Telephone = [result stringForColumn:@"Telephone"];
            contact.MobileTelephone = [result stringForColumn:@"MobileTelephone"];
            contact.Email = [result stringForColumn:@"Email"];
            contact.HeadPortrait = [result stringForColumn:@"HeadPortrait"];
            contact.DEPNAME = [result stringForColumn:@"DEPNAME"];
            contact.SEX = [result stringForColumn:@"SEX"];
            contact.isDel = [result intForColumn:@"isDel"];
            contact.address = [result stringForColumn:@"address"];
            contact.birthDay = [result stringForColumn:@"birthDay"];
            contact.GroupID = [result stringForColumn:@"GroupID"];
            contact.IsAdmin = [result boolForColumn:@"IsAdmin"];
            contact.IsAvailable = [result stringForColumn:@"IsAvailable"];
            contact.pcAvailable = [result stringForColumn:@"pcAvailable"];
            contact.IsOftenContact = [result intForColumn:@"IsOftenContact"];
            contact.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
            contact.info = [result stringForColumn:@"info"];
            contact.img = [result stringForColumn:@"img"];
             contact.Source = [result stringForColumn:@"Source"];
            contact.PinYin = [result stringForColumn:@"PinYin"];
            contact.messageSettingType = [result intForColumn:@"messageSettingType"];
            contact.companyTelePhone = [result stringForColumn:@"companyTelePhone"];
            contact.compCode = [result stringForColumn:@"compCode"];
            contact.isSelected = [result boolForColumn:@"isSelected"];
            contact.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            contact.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            contact.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];

            [contactList addObject:contact];
            contact = nil;
        }
        [result close];
    }];
    return contactList;
}

-(NSArray *)loadContactUserIdAndDepIdByDeptID:(NSString *)departID
{
    if(M_ISSTRNULL(departID))
    {
        return nil;
    }
    NSString *sql = nil;
    __block NSMutableArray *contactList = [NSMutableArray arrayWithCapacity:10];
    sql = [NSString stringWithFormat:@"select DEPTID,UserID from CONTACT where  DEPTID = \'%@\' AND isDel = 1", departID ];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CContactModel *contact = [[CContactModel alloc] init];
            
            contact.DEPTID = [result stringForColumn:@"DEPTID"];
        
            contact.UserID = [result stringForColumn:@"UserID"];
            
            [contactList addObject:contact];
            contact = nil;
        }
        [result close];
    }];
    return contactList;
}


//- (NSArray *)loadContactByGroupID:(NSString *)GroupID
//{
//
//    if(M_ISSTRNULL(GroupID))
//    {
//        return nil;
//    }
//    NSString *sql = nil;
//    __block NSMutableArray *contactList = [NSMutableArray arrayWithCapacity:10];
//    sql = [NSString stringWithFormat:@"select * from CONTACT where  GroupID = \'%@\'", GroupID ];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
//    [queen inDatabase:^(FMDatabase *db) {
//        FMResultSet *result = [db executeQuery:sql];
//        while ([result next]) {
//            CContactModel *contact = [[CContactModel alloc] init];
//            contact.ContactIndex = [result longLongIntForColumn:@"identifer"];
//            contact.DEPTID = [result stringForColumn:@"DEPTID"];
//            contact.UserName = [result stringForColumn:@"UserName"];
//            contact.UserCode = [result stringForColumn:@"UserCode"];
//            contact.UserID = [result stringForColumn:@"UserID"];
//            contact.EXtenSion = [result stringForColumn:@"Extension"];
//            contact.Autograph = [result stringForColumn:@"Autograph"];
//            contact.Position = [result stringForColumn:@"Position"];
//            contact.RoleID = [result stringForColumn:@"RoleID"];
//            contact.Telephone = [result stringForColumn:@"Telephone"];
//            contact.MobileTelephone = [result stringForColumn:@"MobileTelephone"];
//            contact.Email = [result stringForColumn:@"Email"];
//            contact.HeadPortrait = [result stringForColumn:@"HeadPortrait"];
//            contact.DEPNAME = [result stringForColumn:@"DEPNAME"];
//            contact.SEX = [result stringForColumn:@"SEX"];
//            contact.isDel = [result intForColumn:@"isDel"];
//            contact.address = [result stringForColumn:@"address"];
//            contact.birthDay = [result stringForColumn:@"birthDay"];
//            contact.GroupID = [result stringForColumn:@"GroupID"];
//            contact.IsAdmin = [result boolForColumn:@"IsAdmin"];
//            contact.IsAvailable = [result boolForColumn:@"IsAvailable"];
//            contact.IsOftenContact = [result boolForColumn:@"IsOftenContact"];
//            [contactList addObject:contact];
//            contact = nil;
//        }
//    }];
//    return contactList;
//
//}

- (NSArray *)loadOftenContact
{
    NSString *sql = nil;
    __block NSMutableArray *contactList = [NSMutableArray arrayWithCapacity:10];
    sql = [NSString stringWithFormat:@"select * from CONTACT where IsOftenContact = 1"];
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CContactModel *contact = [[CContactModel alloc] init];
            contact.ContactIndex = [result longLongIntForColumn:@"identifer"];
            contact.DEPTID = [result stringForColumn:@"DEPTID"];
            contact.UserName = [result stringForColumn:@"UserName"];
            contact.UserCode = [result stringForColumn:@"UserCode"];
            contact.UserID = [result stringForColumn:@"UserID"];
            contact.EXtenSion = [result stringForColumn:@"Extension"];
            contact.Autograph = [result stringForColumn:@"Autograph"];
            contact.Position = [result stringForColumn:@"Position"];
            contact.RoleID = [result stringForColumn:@"RoleID"];
            contact.Telephone = [result stringForColumn:@"Telephone"];
            contact.MobileTelephone = [result stringForColumn:@"MobileTelephone"];
            contact.Email = [result stringForColumn:@"Email"];
            contact.HeadPortrait = [result stringForColumn:@"HeadPortrait"];
            contact.DEPNAME = [result stringForColumn:@"DEPNAME"];
            contact.SEX = [result stringForColumn:@"SEX"];
            contact.isDel = [result intForColumn:@"isDel"];
            contact.address = [result stringForColumn:@"address"];
            contact.birthDay = [result stringForColumn:@"birthDay"];
            contact.GroupID = [result stringForColumn:@"GroupID"];
            contact.IsAdmin = [result boolForColumn:@"IsAdmin"];
            contact.IsAvailable = [result stringForColumn:@"IsAvailable"];
            contact.pcAvailable = [result stringForColumn:@"pcAvailable"];
            contact.IsOftenContact = [result intForColumn:@"IsOftenContact"];
            contact.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
            contact.info = [result stringForColumn:@"info"];
            contact.img = [result stringForColumn:@"img"];
            contact.Source = [result stringForColumn:@"Source"];
            contact.PinYin = [result stringForColumn:@"PinYin"];
            contact.firstLetter = [result stringForColumn:@"firstLetter"];
            contact.messageSettingType = [result intForColumn:@"messageSettingType"];
            contact.companyTelePhone = [result stringForColumn:@"companyTelePhone"];
            contact.compCode = [result stringForColumn:@"compCode"];
            contact.isSelected = [result boolForColumn:@"isSelected"];
            contact.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            contact.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            contact.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            
            [contactList addObject:contact];
            contact = nil;
        }
        [result close];
    }];
    return contactList;

}

- (NSArray *)searchContactWithSeachStr:(NSString *)searchStr
{
    NSString *sql = nil;
    __block NSMutableArray *contactList = [NSMutableArray arrayWithCapacity:10];
    if (searchStr.length <= 0) {
        return nil;
    }
    //对于客服联系人而言isdel＝0
    sql = [NSString stringWithFormat:@"select * from CONTACT where (isDel = 1 OR yuLiuZiduan3 = \'%@\' OR yuLiuZiduan3 = \'%@\') AND (UserName LIKE '%%%@%%' OR PinYin LIKE '%%%@%%' OR firstLetter LIKE '%%%@%%')", @"isKefu", @"isWaibu", searchStr,searchStr,searchStr];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CContactModel *contact = [[CContactModel alloc] init];
            contact.ContactIndex = [result longLongIntForColumn:@"identifer"];
            contact.DEPTID = [result stringForColumn:@"DEPTID"];
            contact.UserName = [result stringForColumn:@"UserName"];
            contact.UserCode = [result stringForColumn:@"UserCode"];
            contact.UserID = [result stringForColumn:@"UserID"];
            contact.EXtenSion = [result stringForColumn:@"Extension"];
            contact.Autograph = [result stringForColumn:@"Autograph"];
            contact.Position = [result stringForColumn:@"Position"];
            contact.RoleID = [result stringForColumn:@"RoleID"];
            contact.Telephone = [result stringForColumn:@"Telephone"];
            contact.MobileTelephone = [result stringForColumn:@"MobileTelephone"];
            contact.Email = [result stringForColumn:@"Email"];
            contact.HeadPortrait = [result stringForColumn:@"HeadPortrait"];
            contact.DEPNAME = [result stringForColumn:@"DEPNAME"];
            contact.SEX = [result stringForColumn:@"SEX"];
            contact.isDel = [result intForColumn:@"isDel"];
            contact.address = [result stringForColumn:@"address"];
            contact.birthDay = [result stringForColumn:@"birthDay"];
            contact.GroupID = [result stringForColumn:@"GroupID"];
            contact.IsAdmin = [result boolForColumn:@"IsAdmin"];
            contact.IsAvailable = [result stringForColumn:@"IsAvailable"];
            contact.pcAvailable = [result stringForColumn:@"pcAvailable"];
            contact.IsOftenContact = [result intForColumn:@"IsOftenContact"];
            contact.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
            contact.info = [result stringForColumn:@"info"];
            contact.img = [result stringForColumn:@"img"];
            contact.Source = [result stringForColumn:@"Source"];
            contact.PinYin = [result stringForColumn:@"PinYin"];
            contact.firstLetter = [result stringForColumn:@"firstLetter"];
            contact.messageSettingType = [result intForColumn:@"messageSettingType"];
            contact.companyTelePhone = [result stringForColumn:@"companyTelePhone"];
            contact.compCode = [result stringForColumn:@"compCode"];
            contact.isSelected = [result boolForColumn:@"isSelected"];
            contact.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            contact.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            contact.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            [contactList addObject:contact];
            contact = nil;
        }
        
        [result close];
    }];
    if (contactList.count > 0) {
        return contactList;
    } else {
    
        return nil;
    }

}

- (NSInteger)loadAllcontactCount
{
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block NSInteger count;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        count = [db intForQuery:@"select COUNT(*) from CONTACT where isDel = 1"];
    }];
    return count;
}

- (NSInteger)loadContactCountWithDepartMentId:(NSString *)departId withUserId:(NSString *)userId
{
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block NSInteger count;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        if (M_ISSTRNULL(userId)) {
            if (departId == nil || departId.integerValue == 0) {
                count = [db intForQuery:@"select COUNT(*) from CONTACT where isDel = 1"];
            } else {
                count = [db intForQuery:@"select COUNT(*) from CONTACT where DEPTID = ? AND isDel = 1",departId];
            }
  
        } else {
            if (departId == nil || departId.integerValue == 0) {
                count = [db intForQuery:@"select COUNT(*) from CONTACT where isDel = 1 AND UserID <> ?",userId];
            } else {
                count = [db intForQuery:@"select COUNT(*) from CONTACT where DEPTID = ? AND isDel = 1 AND UserID <> ?",departId,userId];
            }

        }
    }];
    return count;

}

- (BOOL)delContactWithContactId:(NSString *)contactId
{
    if(contactId == nil)
    {
        return NO;
    }
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from CONTACT where UserID = ?", contactId];
    }];
    return ret;

}

- (BOOL)updateContactIsDelWithUserId:(NSString *)userId isDel:(BOOL)isDel
{
//     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE CONTACT SET isDel = ? where UserID = ?",isDel?@1:@0,userId];
        
    }];
    return ret;
    

}

- (BOOL)isAvalableContactWithUserId:(NSString *)userId
{
    if(M_ISSTRNULL(userId))
    {
        return nil;
    }
    __block BOOL isDel = NO;
    NSString *sql = nil;
    sql = [NSString stringWithFormat:@"select isDel,yuLiuZiduan3 from CONTACT where  UserID = \'%@\'", userId ];
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            
            if ([result boolForColumn:@"isDel"] || [[result stringForColumn:@"yuLiuZiduan3"] isEqualToString:@"isKefu"] || [[result stringForColumn:@"yuLiuZiduan3"] isEqualToString:@"isWaibu"]) {
                
                isDel = YES;
            }

        }
        [result close];
    }];
    return isDel;
}

- (BOOL)updateAllContactNotSelected:(NSArray *)contactList
{
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE CONTACT SET IsSelected = 0"];
        
    }];
    return ret;

}
- (BOOL)updateSelectWithContactList:(NSArray *)contactList isSelected:(BOOL)isSelected
{
    if(contactList == nil || contactList.count <= 0)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CContactModel *contact in contactList)
        {
            ret = [db executeUpdate:@"UPDATE CONTACT SET IsSelected = ? where UserID = ?", isSelected?@1:@0, M_STOREVALUE(contact.UserID)];
            if(!ret)
            {
                *rollback = YES;
            }
        }
        
    }];
    return ret;

}
- (BOOL)updateContactSelectedWithContact:(CContactModel *)contact seleceted:(BOOL)isSelected
{
    if(contact == nil)
    {
        return NO;
    }
    //     FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
         ret = [db executeUpdate:@"UPDATE CONTACT SET IsSelected = ? where UserID = ?", isSelected?@1:@0, M_STOREVALUE(contact.UserID)];
    }];
    return ret;

}

- (NSArray *)loadAllSelectedContact
{
    NSString *sql = nil;
    __block NSMutableArray *contactList = [NSMutableArray arrayWithCapacity:10];
    sql = [NSString stringWithFormat:@"select * from CONTACT where  IsSelected = 1 AND isDel = 1"];
    //    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] companyCommonDatabasePath]];
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            CContactModel *contact = [[CContactModel alloc] init];
            contact.ContactIndex = [result longLongIntForColumn:@"identifer"];
            contact.DEPTID = [result stringForColumn:@"DEPTID"];
            contact.UserName = [result stringForColumn:@"UserName"];
            contact.UserCode = [result stringForColumn:@"UserCode"];
            contact.UserID = [result stringForColumn:@"UserID"];
            contact.EXtenSion = [result stringForColumn:@"Extension"];
            contact.Autograph = [result stringForColumn:@"Autograph"];
            contact.Position = [result stringForColumn:@"Position"];
            contact.RoleID = [result stringForColumn:@"RoleID"];
            contact.Telephone = [result stringForColumn:@"Telephone"];
            contact.MobileTelephone = [result stringForColumn:@"MobileTelephone"];
            contact.Email = [result stringForColumn:@"Email"];
            contact.HeadPortrait = [result stringForColumn:@"HeadPortrait"];
            contact.DEPNAME = [result stringForColumn:@"DEPNAME"];
            contact.SEX = [result stringForColumn:@"SEX"];
            contact.isDel = [result intForColumn:@"isDel"];
            contact.address = [result stringForColumn:@"address"];
            contact.birthDay = [result stringForColumn:@"birthDay"];
            contact.GroupID = [result stringForColumn:@"GroupID"];
            contact.IsAdmin = [result boolForColumn:@"IsAdmin"];
            contact.IsAvailable = [result stringForColumn:@"IsAvailable"];
            contact.pcAvailable = [result stringForColumn:@"pcAvailable"];
            contact.IsOftenContact = [result intForColumn:@"IsOftenContact"];
            contact.msgNoticeSet = [result stringForColumn:@"msgNoticeSet"];
            contact.info = [result stringForColumn:@"info"];
            contact.img = [result stringForColumn:@"img"];
            contact.Source = [result stringForColumn:@"Source"];
            contact.PinYin = [result stringForColumn:@"PinYin"];
            contact.messageSettingType = [result intForColumn:@"messageSettingType"];
            contact.companyTelePhone = [result stringForColumn:@"companyTelePhone"];
            contact.compCode = [result stringForColumn:@"compCode"];
            contact.isSelected = [result boolForColumn:@"isSelected"];
            contact.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
            contact.yuLiuZiduan2 = [result intForColumn:@"yuLiuZiduan2"];
            contact.yuLiuZiduan3 = [result stringForColumn:@"yuLiuZiduan3"];
            
            [contactList addObject:contact];
            contact = nil;
        }
        [result close];
    }];
    return contactList;

}

//根据部门ID加载选中联系人的数量
- (NSInteger)loadAllSelectedCountContactWithDepId:(NSString *)depId
{
    __block NSInteger count;
    [[FMShareDataBase shareDataBase].companyShareQueue inDatabase:^(FMDatabase *db) {
        
            if (M_ISSTRNULL(depId)) {
                count = [db intForQuery:@"select COUNT(*) from CONTACT where isDel = 1 AND IsSelected = 1"];
            } else {
                count = [db intForQuery:@"select COUNT(*) from CONTACT where DEPTID = ? AND isDel = 1 AND IsSelected = 1",depId];
            }
    }];
    return count;

}

@end
